# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    # 将一个二叉树转换成只有右子树的链表 要求就地操作
    # 用栈保存左子树节点信息 遇到左子树为空的节点就退栈
    def flatten(self, root):
        """
        :type root: TreeNode
        :rtype: void Do not return anything, modify root in-place instead.
        """
        if root is None:
            return root
        stack = []
        while root.left is not None or root.right is not None or stack:
            if root.right is not None:
                stack.append(root.right)
                root.right = None
            if root.left is None:
                root.right = stack.pop()
                root = root.right
                continue
            root.right = root .left
            root.left = None
            root = root.right
        return root
